<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类目" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入类目"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goods:goods_category:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goods_categoryList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="类目" align="left" prop="name"/>
      <el-table-column label="图片" align="center" prop="image">
        <template slot-scope="scope" v-if="scope.row.pid != 0">
          <el-image
            style="width: 80px; height: 80px"
            :src="scope.row.image"
            :preview-src-list="[scope.row.image]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:goods_category:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.pid == 0"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['goods:goods_category:add']"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:goods_category:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改商品类目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级" prop="pid">
          <treeselect v-model="form.pid" :options="goods_categoryOptions" :normalizer="normalizer" placeholder="请选择上级"/>
        </el-form-item>
        <el-form-item label="类目" prop="name">
          <el-input v-model="form.name" placeholder="请输入类目"/>
        </el-form-item>
        <el-form-item label="图片" v-if="form.pid != 0">
          <el-image class="res-image" v-if="form.image != null && form.image != ''" :src="form.image"
                    :preview-src-list="[form.image]"/>
          <upload-oss-btn ref="upload" :btnText="form.image == null || form.image == ''? '快速上传':'重新上传'"
                          remark="宽:350  高:350"
                          @onUploadResult="onUploadResult"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listGoods_category,
    getGoods_category,
    delGoods_category,
    addGoods_category,
    updateGoods_category
  } from '@/api/goods/goods_category'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import UploadOssBtn from '@/components/UploadOssBtn'

  export default {
    name: 'Goods_category',
    components: {
      Treeselect, UploadOssBtn
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 商品类目表格数据
        goods_categoryList: [],
        // 商品类目树选项
        goods_categoryOptions: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 状态;0=正常,1=停用字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pid: null,
          name: null,
          image: null,
          status: null
          , orderByColumn: 't.create_time'
          , isAsc: 'desc'
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
      this.getDicts('goods_status').then(response => {
        this.statusOptions = response.data
      })
    },
    methods: {
      onUploadResult(image) {
        this.form.image = image
      },
      /** 查询商品类目列表 */
      getList() {
        this.loading = true
        listGoods_category(this.queryParams).then(response => {
          this.goods_categoryList = this.handleTree(response.data, 'id', 'pid')
          this.loading = false
        })
      },
      /** 转换商品类目数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      /** 查询商品类目下拉树结构 */
      getTreeselect() {
        listGoods_category({ maxLevel: 1 }).then(response => {
          this.goods_categoryOptions = []
          const data = { id: 0, name: '顶级节点', children: [] }
          data.children = this.handleTree(response.data, 'id', 'pid')
          this.goods_categoryOptions.push(data)
        })
      },
      // 状态;0=正常,1=停用字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          pid: null,
          name: null,
          image: null,
          status: '0',
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          isDelete: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset()
        this.getTreeselect()
        if (row != null && row.id) {
          this.form.pid = row.id
        } else {
          this.form.pid = 0
        }
        this.open = true
        this.title = '添加商品类目'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        this.getTreeselect()
        if (row != null) {
          this.form.pid = row.id
        }
        getGoods_category(row.id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改商品类目'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateGoods_category(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addGoods_category(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除商品类目编号为"' + row.id + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delGoods_category(row.id)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(() => {
        })
      }
    }
  }
</script>
<style lang="scss">
  .res-image {
    width: 120px;
    height: 120px;
    border-radius: 6px;
  }

</style>
